package com.huaxia.controller.product;

import com.huaxia.pojo.product.BranchEvergreen;
import com.huaxia.pojo.product.BranchLuck;
import com.huaxia.pojo.product.SaleDeptEvergreen;
import com.huaxia.pojo.product.SaleDeptLuck;
import com.huaxia.service.product.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * @author wangli 爱我华夏，爱我中华！
 * @date 2020/06/10 15:00
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    
    @Autowired
    private WebApplicationContext webApplicationContext;
    @Autowired
    private ProductService productService;

    @GetMapping("/getBranchEvergreen")
    public List<BranchEvergreen> getBranchEvergreen(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<BranchEvergreen> branchEvergreens = (List<BranchEvergreen>) servletContext.getAttribute("branchEvergreens");
        if(branchEvergreens==null){
            branchEvergreens = productService.getBranchEvergreen();
            servletContext.setAttribute("branchEvergreens",branchEvergreens);
            return branchEvergreens;
        }
        return branchEvergreens;
    }

    @GetMapping("/getSaleDeptEvergreen")
    public List<SaleDeptEvergreen> getSaleDeptEvergreen(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<SaleDeptEvergreen> saleDeptEvergreens = (List<SaleDeptEvergreen>) servletContext.getAttribute("saleDeptEvergreens");
        if(saleDeptEvergreens==null){
            saleDeptEvergreens = productService.getSaleDeptEvergreen();
            servletContext.setAttribute("saleDeptEvergreens",saleDeptEvergreens);
            return saleDeptEvergreens;
        }
        return saleDeptEvergreens;
    }

    @GetMapping("/getBranchLuck")
    public List<BranchLuck> getBranchLuck(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<BranchLuck> branchLucks = (List<BranchLuck>) servletContext.getAttribute("branchLucks");
        if(branchLucks==null){
            branchLucks = productService.getBranchLuck();
            servletContext.setAttribute("branchLucks",branchLucks);
            return branchLucks;
        }
        return branchLucks;
    }

    @GetMapping("/getSaleDeptLuck")
    public List<SaleDeptLuck> getSaleDeptLuck(){
        ServletContext servletContext = webApplicationContext.getServletContext();
        List<SaleDeptLuck> saleDeptLucks = (List<SaleDeptLuck>) servletContext.getAttribute("saleDeptLucks");
        if(saleDeptLucks==null){
            saleDeptLucks = productService.getSaleDeptLuck();
            servletContext.setAttribute("saleDeptLucks",saleDeptLucks);
            return saleDeptLucks;
        }
        return saleDeptLucks;
    }
}
